package com.dianping.cat.alarm.server.transform;

import com.dianping.cat.alarm.server.IEntity;
import com.dianping.cat.alarm.server.IVisitor;
import com.dianping.cat.alarm.server.entity.Condition;
import com.dianping.cat.alarm.server.entity.Rule;
import com.dianping.cat.alarm.server.entity.ServerAlarmRuleConfig;
import com.dianping.cat.alarm.server.entity.SubCondition;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  37 */   public DefaultXmlBuilder() { this(false); }



/*  41 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  45 */     this.m_compact = compact;
/*  46 */     this.m_sb = sb;
/*  47 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  51 */     entity.accept(this.m_visitor);
/*  52 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  56 */     this.m_level--;

/*  58 */     indent();
/*  59 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  63 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  67 */     if (value == null) {
/*  68 */       return null;
    }

/*  71 */     String str = toString(value);
/*  72 */     int len = str.length();
/*  73 */     StringBuilder sb = new StringBuilder(len + 16);

/*  75 */     for (int i = 0; i < len; i++) {
/*  76 */       char ch = str.charAt(i);

      switch (ch) {
        case '<':
/*  80 */           sb.append("&lt;");
          break;
        case '>':
/*  83 */           sb.append("&gt;");
          break;
        case '&':
/*  86 */           sb.append("&amp;");
          break;
        case '"':
/*  89 */           if (!text) {
/*  90 */             sb.append("&quot;");
            break;
          }
        default:
/*  94 */           sb.append(ch);
          break;
      }

    }
/*  99 */     return sb.toString();
  }

  protected void indent() {
/* 103 */     if (!this.m_compact) {
/* 104 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 105 */         this.m_sb.append("   ");
      }
    }
  }


/* 111 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 115 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 119 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 123 */     indent();

/* 125 */     this.m_sb.append('<').append(name);

/* 127 */     int len = nameValues.length;

/* 129 */     for (int i = 0; i + 1 < len; i += 2) {
/* 130 */       Object attrName = nameValues[i];
/* 131 */       Object attrValue = nameValues[i + 1];

/* 133 */       if (attrValue != null) {
/* 134 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 138 */     if (dynamicAttributes != null) {
/* 139 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 140 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 144 */     if (text != null && closed) {
/* 145 */       this.m_sb.append('>');
/* 146 */       this.m_sb.append(escape(text, true));
/* 147 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 149 */       if (closed) {
/* 150 */         this.m_sb.append('/');
      } else {
/* 152 */         this.m_level++;
      }

/* 155 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 161 */     if (value instanceof String)
/* 162 */       return (String)value; 
/* 163 */     if (value instanceof Collection) {
/* 164 */       Collection<Object> list = (Collection)value;
/* 165 */       StringBuilder sb = new StringBuilder(32);
/* 166 */       boolean first = true;

/* 168 */       for (Object item : list) {
/* 169 */         if (first) {
/* 170 */           first = false;
        } else {
/* 172 */           sb.append(',');
        }

/* 175 */         if (item != null) {
/* 176 */           sb.append(item);
        }
      }

/* 180 */       return sb.toString();
/* 181 */     }  if (value.getClass().isArray()) {
/* 182 */       int len = Array.getLength(value);
/* 183 */       StringBuilder sb = new StringBuilder(32);
/* 184 */       boolean first = true;

/* 186 */       for (int i = 0; i < len; i++) {
/* 187 */         Object item = Array.get(value, i);

/* 189 */         if (first) {
/* 190 */           first = false;
        } else {
/* 192 */           sb.append(',');
        }

/* 195 */         if (item != null) {
/* 196 */           sb.append(item);
        }
      }

/* 200 */       return sb.toString();
    }

/* 203 */     return String.valueOf(value);
  }


  public void visitCondition(Condition condition) {
/* 208 */     startTag("condition", null, new Object[] { "interval", condition.getInterval(), "duration", Integer.valueOf(condition.getDuration()), "alert-type", condition.getAlertType() });

/* 210 */     if (!condition.getSubConditions().isEmpty()) {
/* 211 */       for (SubCondition subCondition : condition.getSubConditions()) {
/* 212 */         subCondition.accept(this.m_visitor);
      }
    }

/* 216 */     endTag("condition");
  }


  public void visitRule(Rule rule) {
/* 221 */     startTag("rule", null, new Object[] { "start-time", rule.getStartTime(), "end-time", rule.getEndTime() });

/* 223 */     if (!rule.getConditions().isEmpty()) {
/* 224 */       for (Condition condition : rule.getConditions()) {
/* 225 */         condition.accept(this.m_visitor);
      }
    }

/* 229 */     endTag("rule");
  }


  public void visitServerAlarmRuleConfig(ServerAlarmRuleConfig serverAlarmRuleConfig) {
/* 234 */     startTag("server-alarm-rule-config", null, new Object[] { "id", serverAlarmRuleConfig.getId() });

/* 236 */     if (!serverAlarmRuleConfig.getRules().isEmpty()) {
/* 237 */       for (Rule rule : serverAlarmRuleConfig.getRules()) {
/* 238 */         rule.accept(this.m_visitor);
      }
    }

/* 242 */     endTag("server-alarm-rule-config");
  }



/* 247 */   public void visitSubCondition(SubCondition subCondition) { startTag("sub-condition", true, null, new Object[] { "type", subCondition.getType(), "value", subCondition.getValue() }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/server/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */